﻿Imports Microsoft.VisualBasic
Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports System

Public Class Equipo
    'instanciar la conexion
    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)

    'listado de productos
    Public Function Listado() As DataTable
        Dim da As New SqlDataAdapter("select e.cod_equipo,m.Name_marca,e.Equip_desc,e.tipo_equipo,e.precio from tb_Equipo e inner join tb_marca m on e.cod_marca=m.cod_marca", cn)
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    Public Function ListadoParametro(ByVal nom As String) As DataTable
        Dim da As New SqlDataAdapter(nom, cn)
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    Public Function ListaMarca() As DataTable
        Dim da As New SqlDataAdapter("select * from tb_marca", cn)
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    Public Function ListaEquiposImagen() As DataTable

        Dim da As New SqlDataAdapter("select * from tb_equipo", cn)
        Dim dt As New DataTable
        da.Fill(dt)
        Return dt


    End Function


    Public Function ingresarEquipo(ByVal codigo As String, ByVal nombre As String, ByVal foto As Image) As Integer

        Dim cmd As New SqlCommand("sp_ingresar_equipo", cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@cod", SqlDbType.Int).Value = codigo
        cmd.Parameters.Add("@cod_marca", SqlDbType.VarChar, 100).Value = nombre
        cmd.Parameters.Add("@desc", SqlDbType.VarChar, 100).Value = nombre
        cmd.Parameters.Add("@tipo", SqlDbType.VarChar, 100).Value = nombre
        cmd.Parameters.Add("@precio", SqlDbType.VarChar, 100).Value = nombre

        If Not foto Is Nothing Then
            Dim ms As New MemoryStream
            foto.Save(ms, Imaging.ImageFormat.Jpeg)
            Dim data() As Byte = ms.ToArray
            cmd.Parameters.Add("@img", SqlDbType.Image).Value = data
        Else
            cmd.Parameters.Add("@img", SqlDbType.Image).Value = System.DBNull.Value
        End If



        'valor de retorno
        Dim valorretorno As Integer = 0

        Try
            'abrir conexion
            cn.Open()

            'ejecutar procedure
            cmd.ExecuteNonQuery()

            'valor correcto
            valorretorno = 0

        Catch ex As Exception
            valorretorno = 1
        Finally
            'cerrar conexion
            cn.Close()

        End Try
        ' retornar valor
        Return valorretorno

    End Function


    Public Function InsertarEquipo(ByVal des As String, ByVal cod As String, ByVal tip As String, ByVal pre As Decimal, ByVal ima As String) As Integer
        Try


            Dim cmd As New SqlCommand("usp_registrarEquipo", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@desc", SqlDbType.VarChar).Value = des
            cmd.Parameters.Add("@cod_marca", SqlDbType.VarChar).Value = cod
            cmd.Parameters.Add("@tip", SqlDbType.VarChar).Value = tip
            cmd.Parameters.Add("@precio", SqlDbType.Decimal).Value = pre
            cmd.Parameters.Add("@imagen", SqlDbType.VarChar).Value = ima

            cn.Open()
            cmd.ExecuteNonQuery()


            Return 1

        Catch ex As Exception

            Return 0

        End Try

        cn.Close()

    End Function



End Class
